#!/usr/bin/perl
#convert.pl
#converts a number of wav files in a specified folder to raw files suitable for BOSS 
#uses sox

#Version: 1.2.1
#@autor: Igor Jauk
#@autor: Filip Müllers
#Changelist:
#supports the config class
#uses the file list created by createlist.pl
#checks if socks is installed and quits with an error message if not
#converts wav-to-raw as default, raw-to-wav or raw-to-ogg
#converts all files specified by a filelist or single files

use FindBin qw($Bin);
use lib "$Bin/../libs";
use config;
my $USAGE = "perl convert.pl (-sdir SOUNDDIR -list FILELIST | -f FILE) [-conv (w2r|r2w|r2o) -rate SAMPLINGRATE -h] \n{requires sox. if not available install it (e.g. in Debian systems execute: \"sudo apt-get install sox\")}";
main();

sub main {
	my $cl = config->new(\@ARGV);
	if ($cl->optionflag("-h")) {
		print $USAGE."\n";
		exit;
	}
	select STDERR;

	print "No convert mode specified. Using standard wav-to-raw convertion. For more information execute \"perl convert.pl -h\"\n" if (!$cl->optionflag("-conv"));
	die "No filelist or no file to convert specified. please specify a filelist ir a file to convert.\n$USAGE\n" if (!$cl->optionflag("-list") && !$cl->optionflag("-f"));
	die "Specify a file to convert OR a file list with a target directory to convert all files in the target directory.\n$USAGE\n" if ($cl->optionflag("-list") && $cl->optionflag("-f"));
	
	my $soxcheck = qx(sox --version);
	if (!$soxcheck) {
		die "error: sox not installed.\n$USAGE\n";
	} else {
		print $soxcheck."\n";
	}
	
	my @list;
	my $tdir;
	$tdir = "raw" if ($cl->optionarg("-conv") eq 'w2r' || !$cl->optionflag("-conv"));
	$tdir = "wav" if ($cl->optionarg("-conv") eq 'r2w');
	$tdir = "ogg" if ($cl->optionarg("-conv") eq 'r2o');
	my $wdir;
	if (!$cl->optionflag("-f")) {
		my $listfile = $cl->optionarg("-list");
		open LIST, "$listfile" or die "could not open filelist $list\n";
		chomp(@list = <LIST>);
		close(LIST);
	
		if ($cl->optionflag("-sdir")) {
			$wdir = $cl->optionarg("-sdir");
		} else {
			print "error: no source folder specified.\n";
			die "Usage: $USAGE\naborting\n";
		}
	} else {
		$list[0] = $cl->optionarg("-f");
		$list[0] =~ s/\.wav$//;
		$list[0] =~ s/\.raw$//;
		$list[0] =~ s/\.ogg$//;
	}
	my $rate;
	if ($cl->optionflag("-rate")) {
		$rate = $cl->optionarg("-rate");
	} else {
		print "No sampling rate specified. Using standard sampling rate of 16000 Hz\n";
		$rate = 16000;
	}
	
	process($wdir, $tdir, $rate, \@list, \$cl);
	print "done\n;"
}


sub process {
	my $list = $_[3];
	my $cl = $_[4];
	if (!$$cl->optionflag("-f")) {
		if (!-d "$_[1]") {
			mkdir "$_[1]";
			print "output folder $_[1] created\n";
		} else {
			print "Warning: using existing folder \"$_[1]\" as output folder\n";
		}
	
		foreach $f (@{$list}) {
			qx(sox -t wav $_[0]/$f.wav -r $_[2] $_[1]/$f.raw) if ($$cl->optionarg("-conv") eq "w2r" || !$$cl->optionflag("-conv"));
			qx(sox -t raw -b 16 -r $_[2] -e signed-integer $_[0]/$f.raw $_[1]/$f.wav) if ($$cl->optionarg("-conv") eq "r2w");
			qx(sox -t raw -b 16 -r $_[2] -e signed-integer $_[0]/$f.raw $_[1]/$f.ogg) if ($$cl->optionarg("-conv") eq "r2o");
			print "$f processed\n";
		}
	} else {
		if ($$cl->optionarg("-conv") eq "w2r" || !$$cl->optionflag("-conv")) {
			qx(sox -t wav $list->[0].wav -r $_[2] $list->[0].raw);
		} elsif ($$cl->optionarg("-conv") eq "r2w") {
			qx(sox -t raw -b 16 -r $_[2] -e signed-integer $list->[0].raw $list->[0].wav);
		} elsif ($$cl->optionarg("-conv") eq "r2o") {
			qx(sox -t raw -b 16 -r $_[2] -e signed-integer $list->[0].raw $list->[0].ogg);
		}
		print "$list->[0] processed\n";
	}
}


